<?php

include_once 'db.php';

class Products extends Db {

    public $table_name = "product";

    public function __construct() {
        parent::__construct();
    }
    public function selectcategory($params,$id) {
        /**
         * TODO: Write your code here
         */     
        $query = 'SELECT * FROM '.$this->table_name.' WHERE category = ' . $id;//SQL query 
        if (!empty($params['offset'])) {
            $offset = ($params['offset'] - 1) * PER_PAGE;
            $query .= ' LIMIT ' . $offset . ', ' . PER_PAGE;
        }
        $data = parent::select($query);
        return $data;
    }
    public function countselectcategory($id) {
        $sql = "SELECT COUNT(*) AS COUNT FROM {$this->table_name} WHERE category = " . $id;
        $data = parent::select($sql);
        if (!empty($data[0]['COUNT'])) return (int)$data[0]['COUNT'];
        return 0;
    }
    public function product_coats() {
        $query = 'SELECT * FROM '.$this->table_name.' WHERE category = 11 LIMIT 4';
        $data = parent::select($query);
        return $data;
    }
    public function product_hat() {
        $query = 'SELECT * FROM '.$this->table_name.' WHERE category = 10 LIMIT 4';
        $data = parent::select($query);
        return $data;
    }
    public function product_shoe() {
        $query = 'SELECT * FROM '.$this->table_name.' WHERE category = 9 LIMIT 4';
        $data = parent::select($query);
        return $data;
    }
    public function product_women() {
        $query = 'SELECT * FROM '.$this->table_name.' WHERE category = 5 OR category = 6 OR category = 7 OR category = 8 OR category = 12 LIMIT 4';
        $data = parent::select($query);
        return $data;
    }
    public function product_men() {
        $query = 'SELECT * FROM '.$this->table_name.' WHERE category = 1 OR category = 2 OR category = 3 OR category = 4 LIMIT 4';
        $data = parent::select($query);
        return $data;
    }
    public function selectASC() {
        /**
         * TODO: Write your code here
         */
        
        $query = 'SELECT * FROM '.$this->table_name.' ORDER BY date ASC LIMIT 3';//SQL query
        $data = parent::select($query);
        return $data;
    }
    public function selectDESC() {
        /**
         * TODO: Write your code here
         */
        
        $query = 'SELECT * FROM '.$this->table_name.' ORDER BY date DESC LIMIT 3';//SQL query
        $data = parent::select($query);
        return $data;
    }
    public function selectALL($params) {
        /**
         * TODO: Write your code here
         */
        
        $query = 'SELECT * FROM '.$this->table_name.' ORDER BY date DESC';//SQL query 
        if (!empty($params['offset'])) {
            $offset = ($params['offset'] - 1) * PER_PAGE;
            $query .= ' LIMIT ' . $offset . ', ' . PER_PAGE;
        }
        $data = parent::select($query);
        return $data;
    }
    public function selectoutofstock($params) {
        /**
         * TODO: Write your code here
         */
        
        $query = 'SELECT * FROM '.$this->table_name.' WHERE quantity = 0';//SQL query 
        if (!empty($params['offset'])) {
            $offset = ($params['offset'] - 1) * PER_PAGE;
            $query .= ' LIMIT ' . $offset . ', ' . PER_PAGE;
        }
        $data = parent::select($query);
        return $data;
    }
    public function countoutofstock() {
        $query = "SELECT COUNT(*) AS COUNT FROM {$this->table_name} WHERE quantity = 0";
        $data = parent::select($query);
        if(!empty($data[0]['COUNT']))
            return (int)$data[0]['COUNT'];
        return 0;
    }
    public function insert($name, $price, $category, $quantity, $image) {
        /**
         * TODO: Write your code here
         */
        $date=date("Y-m-d");
        $query = 'INSERT INTO '.$this->table_name.' (id,name,price,category,quantity,date,image) VALUES ("","'.$name.'","'.$price.'","'.$category.'","'.$quantity.'","'.$date.'","'.$image.'")';//SQL query
        $data = parent::insert($query);
        return $data;
    }
    public function update($id, $name, $price, $category, $quantity, $image) {
        /**
         * TODO: Write your code here
         */
        $query = 'UPDATE '.$this->table_name.' SET name = "'.$name.'",price = '.$price.',category = '.$category.',quantity = '.$quantity.',image = "'.$image.'" WHERE id = '.$id;//SQL query
        $data = parent::update($query);
        return $data;
    }
    public function updatename($name, $quantity) {
        /**
         * TODO: Write your code here
         */
        $query = 'UPDATE '.$this->table_name.' SET quantity = '.$quantity.' WHERE name = "'.$name.'"';//SQL query
        var_dump($query);
        $data = parent::update($query);
        return $data;
    }
    public function delete($id) {
        /**
         * TODO: Write your code here
         */
        $query = 'DELETE FROM '.$this->table_name.' WHERE id = '.$id;//SQL query
        $data = parent::delete($query);
        return $data;
    }

    public function search($params,$name)
    {
        $query = "SELECT * FROM {$this->table_name} WHERE name LIKE '%$name%'";
        if(!empty($params['offset']))
        {
            $offset = ($params['offset'] - 1) * PER_PAGE;
            $query .= ' LIMIT ' .$offset. ', '.PER_PAGE;
        }
        $data = parent::select($query);
        return $data;
    }
    public function countproductALL() {
        $sql = "SELECT COUNT(*) AS COUNT FROM {$this->table_name}";
        $data = parent::select($sql);
        if (!empty($data[0]['COUNT'])) return (int)$data[0]['COUNT'];
        return 0;
    }
	public function countsearch($name)
    {
        $query = "SELECT COUNT(*) AS COUNT FROM {$this->table_name} WHERE name LIKE '%$name%'";
        $data = parent::select($query);
        if(!empty($data[0]['COUNT']))
            return (int)$data[0]['COUNT'];
        return 0;
    }
    public function selectcart($name)
    {
        $query = 'SELECT * FROM '.$this->table_name.' WHERE name = "'.$name.'"';
        $data = parent::select($query);
        return $data;
    }
    public function selectid($id)
    {
        $query = 'SELECT * FROM '.$this->table_name.' WHERE id = "'.$id.'"';
        $data = parent::select($query);
        return $data;
    }
    public function selectname($name)
    {
        $query = 'SELECT * FROM '.$this->table_name.' WHERE name = "'.$name.'"';
        $data = parent::select($query);
        return $data;
    }
}
